package com.cal.calendar;

import java.util.Calendar;
import java.util.Date;

import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

public class GestureListener extends SimpleOnGestureListener {

	private static final int MIN_FLING_LENGTH_PIX = 120;

	private CalendarWidget calendarWidget;
	
	private Calendar selectedCalendar = Calendar.getInstance();

	// 动画效果
	private Animation pushLeftIn;
	private Animation pushLeftOut;
	private Animation pushRightIn;
	private Animation pushRightOut;

	public GestureListener(CalendarWidget calendarWidget) {
		this.calendarWidget = calendarWidget;

		pushLeftIn = AnimationUtils.loadAnimation(calendarWidget,
				R.anim.push_left_in);
		pushLeftOut = AnimationUtils.loadAnimation(calendarWidget,
				R.anim.push_left_out);
		pushRightIn = AnimationUtils.loadAnimation(calendarWidget,
				R.anim.push_right_in);
		pushRightOut = AnimationUtils.loadAnimation(calendarWidget,
				R.anim.push_right_out);

		pushLeftIn.setAnimationListener(animationListener);
		pushLeftOut.setAnimationListener(animationListener);
		pushRightIn.setAnimationListener(animationListener);
		pushRightOut.setAnimationListener(animationListener);
	}

	// 动画监听类
	private AnimationListener animationListener = new AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {
			return;
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			return;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			calendarWidget.CreateGirdView(calendarWidget.calendarStartDay);
		}
	};

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// ListView lv = getListView();
		// 得到当前选中的是第几个单元格
		int pos = calendarWidget.currentMonthView.pointToPosition((int) e.getX(), (int) e.getY());
		
		LinearLayout selectedDay = (LinearLayout) calendarWidget.currentMonthView.findViewById(pos + 2000);
		
		if (selectedDay != null) {
			if (selectedDay.getTag() != null) {
				
				Date day = (Date) selectedDay.getTag();
				
				selectedCalendar.setTime(day);

				calendarWidget.preMonthViewAdapter.setSelectMonth(selectedCalendar);
				calendarWidget.preMonthViewAdapter.notifyDataSetChanged();

				calendarWidget.currentMonthViewAdapter.setSelectMonth(selectedCalendar);
				calendarWidget.currentMonthViewAdapter.notifyDataSetChanged();

				calendarWidget.latterMonthViewAdapter.setSelectMonth(selectedCalendar);
				calendarWidget.latterMonthViewAdapter.notifyDataSetChanged();
			}
		}

		Log.i("GestureListener", "点击监听position = " + pos);

		return false;
	}

	/**
	 * 用户按下触摸屏,快速移动后松开,由一个MotionEvent ACTION_DOWN事件,
	 * 多个ACTION_MOVE事件,一个ACTION_UP事件触发
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		// 从右往左滑动
		if (e1.getX() - e2.getX() > MIN_FLING_LENGTH_PIX) {
			calendarWidget.viewFlipper.setInAnimation(pushLeftIn);
			calendarWidget.viewFlipper.setOutAnimation(pushLeftOut);
			calendarWidget.viewFlipper.showNext();
			calendarWidget.setNextViewItem();

			return true;

		} else if (e2.getX() - e1.getX() > MIN_FLING_LENGTH_PIX) { // 从左往右滑动
			calendarWidget.viewFlipper.setInAnimation(pushRightIn);
			calendarWidget.viewFlipper.setOutAnimation(pushRightOut);
			calendarWidget.viewFlipper.showPrevious();
			calendarWidget.setPrevViewItem();

			return true;
		}

		return false;
	}
}
